
import java.net.Socket;
import java.io.OutputStream;
import java.io.InputStream;

public class TcpClient {
    public static void main(String[] args) throws Exception{
        //创建客户端对象 连接服务器
        //指定服务器IP 端口
        //注意这里的ip和端口都是服务器规定好的
        Socket client=new Socket("127.0.0.1",8888);
        System.out.println("客户端："+client);
        //给服务器端发数据
        //获取IO流
        InputStream is = client.getInputStream();
        OutputStream os = client.getOutputStream();
        //给服务器端发送数据
        String string = "hello,server,我是Client";
        //字符串转byte类型数组
        byte[] b = string.getBytes();
        os.write(b);
        byte[] a = new byte[1024];
        int length = is.read(a);
        if(length != -1){
            System.out.println(new String(a,0,length));
        }
        //客户端解析服务端的消息
        //关闭资源
        os.close();
        is.close();
        client.close();

    }
}